#include "rolltext.h"
#include "ui_rolltext.h"
#include <QPainter>
#include <QFontMetrics>
#include <QDebug>
rolltext::rolltext(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::rolltext)
{
    ui->setupUi(this);
    connect(&mtimer, SIGNAL(timeout()), this, SLOT(update()));
    mtimer.start(100);

    x = this->width();
    y = this->height()/2;
}

rolltext::~rolltext()
{
    delete ui;
}

void rolltext::paintEvent(QPaintEvent *event)
{
    //qDebug()<<"111";
    //计算text长度--把字符串长度换算为像素长度
    QFontMetrics metrics(this->font());
    int tsize = metrics.width(text);

    QPainter painter(this);
    x-=5;
    if(x < 0)
    {
        painter.drawText(x, y, text);
        painter.drawText(this->width()+x, y, text);
        if(x < -tsize) x = this->width()-tsize;
    }else {
        painter.drawText(x, y, text);
    }
}

QString rolltext::getText() const
{
    return text;
}

void rolltext::setText(const QString &value)
{
    text = value;
}
